package com.bdmc.app.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.app.model.AppLoginModel;
import com.bdmc.base.domain.BasePerson;
import com.bdmc.base.service.IBasePersonService;
import com.bdmc.common.core.domain.model.LoginUser;
import com.bdmc.common.enums.DeviceType;
import com.bdmc.common.enums.UserType;
import com.bdmc.common.utils.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppLoginService {

    @Autowired
    IBasePersonService personService;

    public String appLogin(AppLoginModel model){
        LambdaQueryWrapper<BasePerson> w1 = Wrappers.lambdaQuery();
        //w1.eq(BasePerson::getOpenid,model.getOpenid());
        BasePerson person = personService.getOne(w1.eq(BasePerson::getMobile, model.getMobile()));
        if(person!=null){
            LoginUser loginUser = new LoginUser();
            loginUser.setUserId(person.getPersonId());
            loginUser.setDeptId(person.getDeptId());
            loginUser.setUsername(person.getPersonName());
            LoginUtils.loginByDevice(loginUser, UserType.APP_USER, DeviceType.APP);
            // 生成token
            return StpUtil.getTokenValue();
        }
        return "";
    }

    public BasePerson getCurrentLoginPerson(){
        LoginUser user = LoginUtils.getLoginUser();
        return personService.getById(user.getUserId());
    }
}
